import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')

// 引入等比适配插件
const px2rem = require('postcss-pxtorem')

// https://vitejs.dev/config/
export default defineConfig({
   plugins: [vue()],

   resolve: {
      alias: {
         "@": path.resolve(__dirname, "src"),
      },
      extensions: [".js", ".ts", ".tsx", ".jsx"],

   },
   base: './', // 打包路径
   css: {
      // 配置全局样式
      preprocessorOptions: {
         scss: {
            charset: false,
            additionalData: `@import "@/assets/styles/common.scss";`
         }
      },
      postcss: {
         plugins: [
            // 去除 @charset 警告
            {
               postcssPlugin: 'internal:charset-removal',
               AtRule: {
                  charset: (atRule) => {
                     if (atRule.name === 'charset') {
                        atRule.remove();
                     }
                  }
               }
            },
            // 自适应配置，移动端设置rootValue 37.5即可
            px2rem({ rootValue: 16, unitPrecision: 2, propList: ['*'] })
         ]
      }
   },
   server: {
      host: '0.0.0.0',
      https: false,
      port: 8080, // 启动端口
      open: true,
      proxy: {
         '/api': { //这个名字与 http.ts 中的 baseURL 一定要一致，切记！
            target: 'https://restapi.amap.com/v3/',
            changeOrigin: true,
            ws: true,
            rewrite: path => path.replace(/^\/api/, '')
         }
      }
   },
   build: {
      // 生产环境移除 console
      terserOptions: {
         compress: {
            drop_console: true,
            drop_debugger: true
         }
      }
   }
})
